package com.globallogic.socialnetwork.jsf.bean;

import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;

import com.globallogic.socialnetwork.entity.Message;
import com.globallogic.socialnetwork.service.UserService;
import com.globallogic.socialnetwork.util.DaoException;
import com.globallogic.socialnetwork.util.WebApplication;

/**
 * JSF Bean to handle all functions associated with messages
 */
@Named("msgBean")
@Scope("request")
public class MessageBean {

	@Inject
	@Named("userService")
	private UserService userService;
	private String userLogin;
	private String receiver;
	private String message;
	private List<Message> messages;
	private boolean haveUnreadMessage;
	private Message selectedMessage;

	private final static Logger log = Logger
			.getLogger("SocialNetworkFileLogger");

	public MessageBean() {
		userLogin = WebApplication.getSessionAttribute("userlogin").toString();
	}

	/**
	 * Updates list of messages
	 */
	public void refreshMessages() {
		try {
			setMessages(userService.getAllMessages(userLogin));
		} catch (DaoException e) {
			log.warn("");
		}
	}

	/**
	 * Send message to <receiver> with content <message>
	 * 
	 * @return "home?faces-redirect=true"
	 */
	public String sendMessage() {
		try {
			if (!receiver.isEmpty() && !message.isEmpty()) {
				log.warn("Sending message: " + message + " to " + receiver);
				String sender = userLogin;
				userService.sendMessage(sender, receiver, message);
				refreshMessages();
			}
		} catch (DaoException e) {
			log.warn("");
		}
		return "home?faces-redirect=true";
	}

	/**
	 * Removes selected message
	 */
	public void removeMessage() {
		try {
			if (selectedMessage != null)
				userService.removeMessage(selectedMessage);
		} catch (DaoException e) {
			log.warn(e);
		}
	}

	/**
	 * Marks as read message <selectedMessage>
	 */
	public void markAsRead() {
		try {
			userService.markAsRead(selectedMessage);
		} catch (DaoException e) {
			log.warn(e);
		}

	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public String getUserLogin() {
		return userLogin;
	}

	public void setUserLogin(String userLogin) {
		this.userLogin = userLogin;
	}

	public List<Message> getMessages() {
		try {
			messages = userService.getAllMessages(userLogin);
			haveUnreadMessage = isHaveUnreadMessage();
		} catch (DaoException e) {
			log.warn(e);
		}
		return messages;
	}

	public Message getSelectedMessage() {
		return selectedMessage;
	}

	public void setSelectedMessage(Message selectedMessage) {
		this.selectedMessage = selectedMessage;
	}

	public void setMessages(List<Message> messages) {
		this.messages = messages;
	}

	public boolean isHaveUnreadMessage() {
		log.error(getMessage());
		if (getMessage() != null)
			for (Message msg : messages)
				if (msg.isNew())
					return true;

		return false;
	}

	public void setHaveUnreadMessage(boolean haveUnreadMessage) {
		this.haveUnreadMessage = haveUnreadMessage;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getReceiver() {
		return receiver;
	}

	public void setReceiver(String receiver) {
		this.receiver = receiver;
	}
}